import xhr from 'SERVICE/xhr/';
import {Toast} from 'antd-mobile';



/*手机号验证*/
export function cellphoneTest(str) {
    var pattern = /^1[2|3|4|5|6|7|8|9]\d{9}$/;
    if(pattern.test(str)){
        return true;
    }else{
        Toast.info('请输入正确的手机号',2, null, false);
        return false;
    }
}

/*密码验证*/
export function passwordTest(str) {
	
}

/*身份证验证*/
export function IDTest(str) {
   var pattern = /\d{17}[\d|x]|\d{15}/;
    if(pattern.test(str)){
        return true;
    }else{
        Toast.info('身份证格式不正确',2, null, false);
    }
}

/*用户名验证*/
export function userNameTest(str) {
    
}

/*邮箱验证*/
export function emailTest(str) {
    var pattern = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
    if(pattern.test(str)){
        return true;
    }else{
        Toast.info('邮箱格式不正确',2, null, false);
    }
}

/*获取手机验证码*/
export function getPhoneCode(phone,obj,url) {
    let countDown = ()=>{
        var data = {};
        if(!obj.hasClass("getCode")){
            obj.addClass("getCode");
            var second = 120;
            var beginTime=new Date().getTime();
            var time = setInterval(function(){
                var newTime=new Date().getTime();
                var dTime=(newTime-beginTime)/1000;
                dTime=parseInt(dTime);
                if(second>=1){
                    second = 120-dTime;
                    obj.text('['+second+'S重新发送]');
                }else{
                    clearInterval(time);
                    obj.removeClass('getCode');
                    obj.text('[重新发送]');
                }
            },1000);
        }
    }
    if (!obj.hasClass("getCode")) {
        return xhr({
          url: url,
          body: {
            code:'MC0101',
            mobileNo:phone,
            flag:'mobileChange'
          }
        }).then((res)=>{
            localStorage.setItem('uuid',res.uuid)
            countDown()
            Toast.info('发送成功',2, null, false);
        })
    }
}

